﻿using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Forum.Model.Response
{
    public class ApiResponse<T> : IApiResponse<T>
    {
        public ApiResponse()
        {

        }
        public ApiResponse(T? result)
        {
            data = result;
        }

        public int code { get; set; } = StatusCodes.Status200OK;
        public string? msg { get; set; }
        public T? data { get; set; }

        /// <summary>
        /// 将ApiResponse隐式转化成ApiResponse<T>
        /// </summary>
        /// <param name="apiResponse"></param>
        public static implicit operator ApiResponse<T>(ApiResponse apiResponse)
        {
            return new ApiResponse<T>
            {
                code = apiResponse.code,
                msg = apiResponse.msg
            };
        }
    }

    public class ApiResponse : IApiResponse
    {
        public ApiResponse(int enumCode, string message, object? result)
        {
            code = enumCode;
            msg = message;
            data = result;
        }

        public int code { get; set; } = 0;
        public object? data { get; set; }
        public string? msg { get; set; }

        public static ApiResponse Error(string message)
            => new ApiResponse(StatusCodes.Status500InternalServerError, message, null);

        public static ApiResponse OK(string message = "", object? data = null)
            => new ApiResponse(StatusCodes.Status200OK, message, data);

        public static ApiResponse NotFound(string message = "NotFound")
           => new ApiResponse(StatusCodes.Status404NotFound, message, null);

        public static ApiResponse BadRequest(string message = "BadRequest")
            => new ApiResponse(StatusCodes.Status400BadRequest, message, null);

        public static ApiResponse Unauthorized(string message = "Unauthorized")
            => new ApiResponse(StatusCodes.Status401Unauthorized, message, null);
    }

    public interface IApiResponse
    {
        public int code { get; set; }
        public string? msg { get; set; }
    }

    public interface IApiResponse<T> : IApiResponse
    {
        public T? data { get; set; }
    }
}

